using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace MTRC.ODMS.Web.SOM.CodeSetup
{
    /// <summary>
    /// Author: XiaoChunhu
    /// </summary>
    public partial class CleanShiftCodeEntry : BasePage_SOM
    {
        #region MenuID

        protected override string MenuID
        {
            get
            {
                return "M140812";//set the Menu
            }
        }

        #endregion

        #region Page Init And Load

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ChangeLanguage += new ChangeLanguageEventHandler(SetLanguage_Click);//add event to chanage the language
        }

        #endregion

        #region page load and set the page lanuage

        //when the page load,these funtion will be run
        protected void Page_Load(object sender, EventArgs e)
        {
            this.TracePageChange = true;//allow to chanage the page
            if (!IsPostBack)
            {
                SetLanguage();
                SetPageLoad();
                this.btnNew.Visible = false;
            }

        }

        //when you chanage the language,this function will be run
        void SetLanguage_Click(object sender, EventArgs e)
        {
            SetLanguage();
        }

        //get the word for the page and by the lanuage
        private void SetLanguage()
        {
            this.lblTitle.Text = this.GetLabel("SOM_ShiftCodeEntryTitle");
            this.lblShiftCode1.Text = this.GetLabel("SOM_ShiftEntryCode1", true);
            this.lblShiftCode2.Text = this.GetLabel("SOM_ShiftEntryCode2", true);
            this.lblShiftCode3.Text = this.GetLabel("SOM_ShiftEntryCode3", true);
            this.lblOrder.Text = this.GetLabel("SOM_ShiftCodeEntryOrder", true);
            this.lblVoid.Text = this.GetLabel("SOM_Void", true);
            this.btnSave.Text = this.GetLabel("COM_Save");
            this.btnNew.Text = this.GetLabel("SOM_ShiftCodeEntryAddNew");
            this.btnSaveReturn.Text = this.GetLabel("COM_SaveAndReturn");
            this.btnSaveNew.Text = this.GetLabel("SOM_SaveNew");
            this.btnCancel.Text = this.GetLabel("COM_Cancel");
        }

        /// <summary>
        /// at the pageload this function will run, and if nothing send here,
        /// it will load a new page and if something send here it will bind some data on the control by the CleanShiftID
        /// </summary>
        private void SetPageLoad()
        {
            if (this.Request.QueryString["CleanShiftID"] != null)//is something send here 
            {
                txtSession.Text = this.Request.QueryString["CleanShiftID"].Trim();
                //get datatable by the CleanShiftID
                DataTable dt = MTRC.ODMS.DAL.SOM.CodeSetup.CDCleanShift.GetShiftByKey(int.Parse(this.Request.QueryString["CleanShiftID"]));
                if (MTRC.ODMS.DAL.COM.Util.BooleanFromDB(dt.Rows[0]["FlagObsolete"]))
                {
                    txtShiftCode1.Text = MTRC.ODMS.DAL.COM.Util.StringFromDB(dt.Rows[0]["Code1"]);//bind the Code1 on the control
                    txtShiftCode2.Text = MTRC.ODMS.DAL.COM.Util.StringFromDB(dt.Rows[0]["Code2"]);//bind the Code2 on the control
                    txtShiftCode3.Text = MTRC.ODMS.DAL.COM.Util.StringFromDB(dt.Rows[0]["Code3"]);//bind the Code3 on the control
                    txtOrder.Text = MTRC.ODMS.DAL.COM.Util.StringFromDB(dt.Rows[0]["Order"]);//bind the Order on the control
                    chVoid.Checked = MTRC.ODMS.DAL.COM.Util.BooleanFromDB(dt.Rows[0]["FlagObsolete"]);//bind the FlagObsolete on the control
                    txtOrder.Enabled = false;
                    txtShiftCode1.Enabled = false;
                    txtShiftCode2.Enabled = false;
                    txtShiftCode3.Enabled = false;
                    chVoid.Enabled = false;
                    btnSave.Enabled = false;
                    btnSaveNew.Enabled = false;
                    btnSaveReturn.Enabled = false;
                }
                else
                {
                    txtShiftCode1.Text = MTRC.ODMS.DAL.COM.Util.StringFromDB(dt.Rows[0]["Code1"]);//bind the Code1 on the control
                    txtShiftCode2.Text = MTRC.ODMS.DAL.COM.Util.StringFromDB(dt.Rows[0]["Code2"]);//bind the Code2 on the control
                    txtShiftCode3.Text = MTRC.ODMS.DAL.COM.Util.StringFromDB(dt.Rows[0]["Code3"]);//bind the Code3 on the control
                    txtOrder.Text = MTRC.ODMS.DAL.COM.Util.StringFromDB(dt.Rows[0]["Order"]);//bind the Order on the control
                    chVoid.Checked = MTRC.ODMS.DAL.COM.Util.BooleanFromDB(dt.Rows[0]["FlagObsolete"]);//bind the FlagObsolete on the control
                }
            }
            //add the message on the button,when you click the button,it will to check the control and show the message
            this.btnSave.Attributes.Add("onclick", "return CheckData('" +
                                                                            this.GetMessage("SOM_CleanShiftCode1") + "','" +
                                                                            this.GetMessage("SOM_CleanShiftCode2") + "','" +
                                                                            this.GetMessage("SOM_CleanShiftCode3") + "','" +
                                                                            this.GetMessage("SOM_CleanShiftOrder") + "')");
            this.btnSaveNew.Attributes.Add("onclick", "return CheckData('" +
                                                                        this.GetMessage("SOM_CleanShiftCode1") + "','" +
                                                                            this.GetMessage("SOM_CleanShiftCode2") + "','" +
                                                                            this.GetMessage("SOM_CleanShiftCode3") + "','" +
                                                                            this.GetMessage("SOM_CleanShiftOrder") + "')");
            this.btnSaveReturn.Attributes.Add("onclick", "return CheckData('" +
                                                                        this.GetMessage("SOM_CleanShiftCode1") + "','" +
                                                                            this.GetMessage("SOM_CleanShiftCode2") + "','" +
                                                                            this.GetMessage("SOM_CleanShiftCode3") + "','" +
                                                                            this.GetMessage("SOM_CleanShiftOrder") + "')");
        }

        #endregion

        #region Bind Some Data

        private int GetCleanShiftID()
        {
            //this.Request.QueryString["CleanShiftID"].Trim();
            int cleanShiftID = 0;
            int.TryParse(string.Concat( this.Request.QueryString["CleanShiftID"]).Trim(), out cleanShiftID);
            return cleanShiftID;
        }

        //use this function can do insert or update
        private bool Save()
        {
            int action = 1;

            #region Modified for QC466 by ZBH on 2012-Jan-19
            //if (this.GetCleanShiftID() > 0)
            //{
            //    action = 2;
            //}

            int currentCleanShiftID = this.GetCleanShiftID();

            //Scenario 1: came from the List page, so we have the QueryString["CleanShiftID"]
            if (currentCleanShiftID> 0)
            { 
                action = 2;
            }
            //Scenario 2: after inserting, we don't have QueryString["CleanShiftID"], 
            //but we have the added CleanShiftID stored in txtSeeion
            else if (!string.IsNullOrEmpty(txtSession.Text.Trim()))
            {
                currentCleanShiftID = int.Parse(txtSession.Text.Trim());
                action = 2;
            }
            #endregion

            try
            {
                //update to table,when there is something be sent here,click the save will to update the table
                MTRC.ODMS.DAL.SOM.CodeSetup.CDCleanShift.UpdateCleanShift(
                    currentCleanShiftID,
                    txtShiftCode1.Text,
                    txtShiftCode2.Text,
                    txtShiftCode3.Text,
                    int.Parse(txtOrder.Text),
                    chVoid.Checked,
                    this.ODMUser.ID,//get the user id at now
                    MTRC.ODMS.DAL.COM.Util.GetCurrentSystemTime(),
                    this.ODMUser.ID,
                    MTRC.ODMS.DAL.COM.Util.GetCurrentSystemTime(),
                    this.ODMUser.ID,
                    action
                    );
                this.Alert(this.GetMessage("SOM_CleanShiftSuccessful"));
                AfterVoid();
                return true;
            }
            catch
            {
                this.Alert(this.GetMessage("SOM_CleanShiftFailing"));
                return false;
            }
        }

        private void AfterVoid()
        {
            if (chVoid.Checked)
            {
                txtShiftCode1.Enabled = false;
                txtShiftCode2.Enabled = false;
                txtShiftCode3.Enabled = false;
                txtOrder.Enabled = false; ;
                chVoid.Enabled = false;
                btnSave.Enabled = false;
                btnSaveNew.Enabled = false;
                btnSaveReturn.Enabled = false;
            }
        }

        private bool CheckData()
        {
            //check station: must exists in table SOM_CDStation
            //update by gordon 20151019  U247 Can not Test 
            //if (!DAL.SOM.Event.StationEventDa.IsLegalStation(this.ODMUser.StationID))
            if (!DAL.SOM.Event.StationEventDa.IsLegalStation(this.ODMUser.LocationID))
            {
                //this.Alert("Current station is not an legal station for SOM");
                this.Alert(this.GetMessage("SOM_LegalStationCheck"));
                return false;
            }
            if (int.Parse(txtOrder.Text.Trim()) < 0)
            {
                Alert(this.GetMessage("SOM_NumberIsInt"));
                this.txtOrder.Focus();
                return false;
            }
             
            return true;
        }

        //check the order is exist
        private bool SameOrder(int action)
        {
            if (txtOrder.Text.Length > 0)
            {
                DataTable dt = MTRC.ODMS.DAL.SOM.CodeSetup.CDCleanShift.CheckShiftOrder(int.Parse(txtOrder.Text.Trim()));
                //action = 1 is insert and 2 is update
                if (action == 1)
                {
                    //get the data by the order
                    if (dt.Rows.Count > 0)//if the count more than 0, there is a same data in the database 
                    {
                        this.Alert(this.GetMessage("SOM_SameDataInBase"));
                        this.txtOrder.Focus();
                        return false;
                    }
                }
                else if (action == 2)
                {
                    if (dt.Rows.Count > 0)//if the count more than 0, there is a same data in the database
                    {
                        if (txtSession.Text.Trim() != dt.Rows[0]["CleanShiftID"].ToString())
                        {
                            if (txtOrder.Text.Trim() == dt.Rows[0]["Order"].ToString())
                            {
                                this.Alert(this.GetMessage("SOM_SameDataInBase"));
                                this.txtOrder.Focus();
                                return false;
                            }
                        }
                    }
                }
            }
            return true;
        }
        //this function will to check you wang to insert data exist
        //Edit By XiaoChunhu   2008/11/20
        //private bool CheckUnique(bool insert)
        //{
        //    //get the Count,if the Count more than 0, you wang to insert data is exist,you can't to do insert
        //    int DataCount = MTRC.ODMS.DAL.SOM.CodeSetup.CDCleanShift.GetMessageToInsert(txtShiftCode1.Text.Trim(),
        //        txtShiftCode2.Text.Trim(),
        //        txtShiftCode3.Text.Trim(),
        //        int.Parse(txtOrder.Text.Trim())).Rows.Count;
        //    if (DataCount > 0)
        //    {
        //        this.Alert(string.Format(this.GetMessage("SOM_SameDataInBase")));
        //        return false;
        //    }
        //    return true;
        //}

        /// <summary>
        /// clear the page,if there are some data on the page,then these data will lost
        /// </summary>
        private void Clear()
        {
            //clear the data
            this.txtSession.Text = "";
            this.txtOrder.Text = "";
            this.txtShiftCode1.Text = "";
            this.txtShiftCode2.Text = "";
            this.txtShiftCode3.Text = "";
            this.chVoid.Checked = false;
            //set the Enabled,if there are some control's Enabled is false,then these Enabled will chanage to true
            this.chVoid.Enabled = true;
            this.txtOrder.Enabled = true;
            this.txtShiftCode1.Enabled = true;
            this.txtShiftCode2.Enabled = true;
            this.txtShiftCode3.Enabled = true;
        }

        #endregion

        #region some event

        /// <summary>
        /// //when you want to create a new data,and there are some data on the page,you can click the "new"
        /// </summary>
        protected void btnNew_Click(object sender, EventArgs e)
        {
            Clear();//use the Clear function to have a new page
        }

        /// <summary>
        /// //when you write some data on the page,and you want to save these data,you can click the "Save"
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckData())
            {
                return;
            }
            //is there something be sent here,if have,it will do update,if no, it will do insert
            if (txtSession.Text != "")
            {
                if (SameOrder(2))
                {
                    Save();
                }
            }
            //to use a function to do the check the data exist
            else 
            {
                if (SameOrder(1))
                {
                    Save();
                    DataTable dt = MTRC.ODMS.DAL.SOM.CodeSetup.CDCleanShift.CheckShiftOrder(int.Parse(txtOrder.Text.Trim()));
                    if (dt.Rows.Count > 0)
                    {
                        txtSession.Text = dt.Rows[0]["CleanShiftID"].ToString().Trim();
                    }
                }
            }

            
        }

        //when you write some data on the page,and you want to save these data and return the last page,you can click the "Save And Return"
        protected void btnSaveReturn_Click(object sender, EventArgs e)
        {
            if (!CheckData())
            {
                return;
            }
            //is there something be sent here,if have,it will do update,if no, it will do insert
            if (txtSession.Text != "")
            {
                if (SameOrder(2))
                {
                    Save();
                    //to return the last page
                    string script = "<script language=javascript>function btnChange_Click(){window.location.href='CleanShiftCodeList.aspx'} window.setTimeout(btnChange_Click,0);" + "</script>";
                    this.Page.RegisterStartupScript("aaa", script);
                }
            }
            //to use a function to do the check the data exist
            else 
            {
                if (SameOrder(1))
                {
                    Save();
                }
                //to return the last page
                string script = "<script language=javascript>function btnChange_Click(){window.location.href='CleanShiftCodeList.aspx'} window.setTimeout(btnChange_Click,0);" + "</script>";
                this.Page.RegisterStartupScript("aaa", script);
            }
        }

        //when you write some data on the page,and you want to save these data and you have other data want to insert,you can click the "Save And New"
        protected void btnSaveNew_Click(object sender, EventArgs e)
        {
            bool saveSuccess = false;
            if (!CheckData())
            {
                return;
            }
            //is there something be sent here,if have,it will do update,if no, it will do insert
           if (txtSession.Text!="")
            {
                if (SameOrder(2))
                {
                    saveSuccess = Save();
                }
            }
            //to use a function to do the check the data exist
            else 
            {
                if (SameOrder(1))
                {
                    saveSuccess = Save();
                }
            }

            //--added by huangyao 20090119
            if (saveSuccess)
            {
                //to clear the data on the page to insert other data
                Clear();
                string script = "<script language=javascript>function btnChange_Click(){window.location.href='CleanShiftCodeEntry.aspx'} window.setTimeout(btnChange_Click,0);" + "</script>";
                this.Page.RegisterStartupScript("aaa1", script);
            }
            //else
            //{
            //    //this.Alert(this.GetMessage("SOM_CleanShiftFailing"), 1, strURL);
            //}
            //-------end  added by huangyao 20090119
        }

        //if you want to do nothing,and want to return the last page,you can click the "Cancel"
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            //return the last page
            this.Response.Redirect("CleanShiftCodeList.aspx");
        }

        protected void chVoid_CheckedChanged(object sender, EventArgs e)
        {
            if (chVoid.Checked && txtSession.Text != "")
            {
                if (MTRC.ODMS.DAL.SOM.CodeSetup.CDCleanShift.CheckShiftVoid(int.Parse(txtSession.Text.Trim())).Rows.Count > 0)
                {
                    Alert(this.GetMessage("SOM_CleanShiftCodeVoidWarning"));
                    chVoid.Checked = false;
                }
                else
                {
                    Alert(this.GetMessage("SOM_MsgVoid"));
                }
            }
        }

        #endregion
    }
}
